package com.qiaofang.applet.service.property.service.mapstruct;

import com.qiaofang.applet.constants.PropertyTradeTypeConstants;
import com.qiaofang.applet.service.other.util.DateTypeUtil;
import com.qiaofang.applet.service.property.domain.Property;
import com.qiaofang.applet.service.property.service.dto.*;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;
import org.mapstruct.ReportingPolicy;
import org.springframework.data.domain.Page;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;

@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface PropertyMapper {

    SalePropertyListDto domainToSaleListDto(Property property);

    MyPropertyListDto domainToMyHouseListDto(Property property);

    RentPropertyListDto domainToRentListDto(Property property);

    SalePropertyDetailDto domainToSaleDetailDto(Property property);

    RentPropertyDetailDto domainToRentDetailDto(Property property);

    SalePropertyListAdminDto domainToSalePropertyListAdminDto(Property property);

    RentPropertyListAdminDto domainToRentPropertyListAdminDto(Property property);

    List<MyPropertyListDto> domainListToMyPropertyList(List<Property> list);

    default Page<SalePropertyListDto> convertDomainToSaleListDto(Page<Property> propertyPage) {
        List<SalePropertyListDto> content = new ArrayList<>();

        if (propertyPage.getTotalElements() > 0) {
            content = propertyPage.getContent().parallelStream().map(this::domainToSaleListDto).collect(Collectors.toList());
        }

        return PageableExecutionUtils.getPage(content, propertyPage.getPageable(), () -> propertyPage.getTotalElements());
    }

    default Page<RentPropertyListDto> convertDomainToRentListDto(Page<Property> propertyPage) {
        List<RentPropertyListDto> content = new ArrayList<>();

        if (propertyPage.getTotalElements() > 0) {
            content = propertyPage.getContent().parallelStream().map(this::domainToRentListDto).collect(Collectors.toList());
        }

        return PageableExecutionUtils.getPage(content, propertyPage.getPageable(), () -> propertyPage.getTotalElements());
    }

    default Page<SalePropertyListAdminDto> convertDomainToSalePropertyListAdminDto(Page<Property> propertyPage) {
        List<SalePropertyListAdminDto> content = new ArrayList<>();
        if (propertyPage.getTotalElements() > 0) {
            content = propertyPage.getContent().parallelStream().map(this::domainToSalePropertyListAdminDto).collect(Collectors.toList());
        }
        return PageableExecutionUtils.getPage(content, propertyPage.getPageable(), () -> propertyPage.getTotalElements());
    }

    default Page<RentPropertyListAdminDto> convertDomainToRentPropertyListAdminDto(Page<Property> propertyPage) {
        List<RentPropertyListAdminDto> content = new ArrayList<>();
        if (propertyPage.getTotalElements() > 0) {
            content = propertyPage.getContent().parallelStream().map(this::domainToRentPropertyListAdminDto).collect(Collectors.toList());
        }
        return PageableExecutionUtils.getPage(content, propertyPage.getPageable(), () -> propertyPage.getTotalElements());
    }

    default RockPropertyDto domainToRockDetailDto(Property property){
        RockPropertyDto rockDto = new RockPropertyDto();
        if (Objects.nonNull(property)){
            String title = property.getTitle();
            if (StringUtils.isEmpty(property.getTitle())) {
                StringBuffer sb = new StringBuffer();
                sb.append(StringUtils.isNotBlank(property.getEstateName()) ? property.getEstateName() : "")
                        .append(" ")
                        .append((property.getCountF() == null ? "0" : property.getCountF()) + "室")
                        .append((property.getCountT() == null ? "0" : property.getCountT()) + "厅")
                        .append((property.getCountW() == null ? "0" : property.getCountW()) + "卫");
                title = sb.toString();
            }
            rockDto.setTitle(title);
            rockDto.setHouseId(property.getPropertyUniqueUuid());
            rockDto.setAreaNum(Objects.isNull(property.getSquare()) ? "" :
                    property.getSquare().setScale(0, BigDecimal.ROUND_HALF_UP).toPlainString());
            rockDto.setAreaName(property.getDistrictName());
            rockDto.setBlockName(property.getAreaName());
            if (StringUtils.isNotEmpty(property.getMainImageUrl())){
                rockDto.setPhotoUrl(property.getMainImageUrl());
            }else {
                rockDto.setPhotoUrl(property.getCoverPhotoUrl());
            }
            if (PropertyTradeTypeConstants.RENT.equals(property.getTradeType())
                    || PropertyTradeTypeConstants.ALL.equals(property.getTradeType())){
                rockDto.setRentPrice(Objects.isNull(property.getRentPrice()) ? "" :
                        property.getRentPrice().setScale(0, BigDecimal.ROUND_HALF_UP).toPlainString());
                List<String> tags = new LinkedList<>();
                if (Objects.nonNull(property.getRentType())){
                    switch (property.getRentType()){
                        case 1 : tags.add("整租"); break;
                        case 2: tags.add("合租"); break;
                    }
                }
                String direction = property.getDirection();
                if (StringUtils.isNotEmpty(direction)){
                    tags.add(direction.length() == 1 ? "朝" + direction : direction);
                }
                try {
                    if (Objects.nonNull(property.getCreatedTime()) && DateTypeUtil.daysBetween(property.getCreatedTime(), new Date()) < 100){
                        tags.add("新上");
                    }
                } catch (ParseException e) {
                }
                if (Objects.nonNull(property.getElevator()) && property.getElevator()){
                    tags.add("有电梯");
                }
                rockDto.setTags(tags);
            }
            if (PropertyTradeTypeConstants.SALE.equals(property.getTradeType())
                    || PropertyTradeTypeConstants.ALL.equals(property.getTradeType())){
                rockDto.setSalePrice(Objects.isNull(property.getSellPrice()) ? "" :
                        property.getSellPrice().setScale(0, BigDecimal.ROUND_HALF_UP).toPlainString());
            }
            rockDto.setRoomNum(Objects.isNull(property.getCountF()) ? "0" : property.getCountF() + "");
            rockDto.setHallNum(Objects.isNull(property.getCountT()) ? "0" : property.getCountT() + "");
        }
        return rockDto;
    }
}
